package com.jscm.openselect;

import com.App;
import com.jscm.openprocess.OpenProcessListView;
import com.jscm.showdata.SelectDataView;
import com.jscm.showdata.TagMomeory;
import com.scm.all.export.*;
import com.scm.x64.export.MemoryOperationUtilsX64;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.stage.WindowEvent;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 首次搜索
 */
public class SelecthBoxSearchBtnListAddress {
    public static Button button = new Button();

    public static boolean flag;
    public static Thread thread1;
    static {
        init();
    }

    private static void init() {
        button.setPrefWidth(120);
        button.setPrefHeight(40);
        button.setText("开启列表刷新");
        button.setId("SelectBtn");
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    if (flag){
//                        新建的集合
                        ObservableList<TagMomeory> observableList = FXCollections.observableArrayList();
                        observableList.clear();
                        if(SelectDataView.observableList.size()>0){
                            for(int j = 0; j < SelectDataView.observableList.size(); j++){
                                int id = Integer.parseInt(SelectDataView.observableList.get(j).getTypeData());
                                byte[] dataTemp;
                                try {
                                    dataTemp = MemoryOperationUtilsX64.readProcessMemoryByte(OpenProcessListView.processHandler,ByteUtils.hexToLong2(SelectDataView.observableList.get(j).getHexData()),Integer.parseInt(SelectDataView.observableList.get(j).getLen()));
                                }catch (IndexOutOfBoundsException e){
                                    //这里如果出现集合异常就跳出
                                    break;
                                }
                                if(dataTemp !=null && SelectDataView.observableList.get(j)!=null){
                                    //                        划分基本数据类型
//                        这里主要是把完全匹配的数据类型进行进行过滤 可以理解为等号过滤
                                    if(id<= 36  && id > -1){
//                            2字节字符操作
                                        if(id >= 1 && id <= 4){
//                                两字节以内做判断  规划类型转换
                                            char dat = ByteUtils.bytesArrayToChar(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            2字节短整数操作
                                        if(id >= 6 && id <= 9){
//                                两字节以内做判断  规划类型转换
                                            short dat = ByteUtils.bytesArrayToShort1(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            4字节整数操作
                                        if(id >= 11 && id <= 14){
//                                两字节以内做判断  规划类型转换
                                            int dat = ByteUtils.bytesArrayToInt(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            8 字节长整数操作
                                        if(id >= 16 && id <= 19){
//                                两字节以内做判断  规划类型转换
                                            long dat = ByteUtils.bytesArrayToLong1(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            4 字节浮点操作
                                        if(id >= 21 && id <= 24){
//                                两字节以内做判断  规划类型转换
                                            float dat = ByteUtils.bytesArrayToFloat1(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            8 字节双精度整数操作
                                        if(id >= 26 && id <= 29){
//                                两字节以内做判断  规划类型转换
                                            double dat = ByteUtils.bytesArrayToDouble1(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            字节数组操作
                                        if(id == 31){
//                                两字节以内做判断  规划类型转换
                                            byte[] dat = dataTemp;
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),ByteUtils.bytesToHexString(dat),SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            特征字节数组操作
                                        if(id == 32){
//                                两字节以内做判断  规划类型转换
                                            byte[] dat = dataTemp;
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),ByteUtils.bytesToHexString(dat),SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            Utf8操作
                                        if(id == 34){
//                                两字节以内做判断  规划类型转换
                                            String dat = ByteUtils.bytesArrayToUtf8(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            UniCode操作
                                        if(id == 35){
//                                两字节以内做判断  规划类型转换
                                            String dat = ByteUtils.bytesArrayToUtf8(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
//                            ASCII操作
                                        if(id == 36){
//                                两字节以内做判断  规划类型转换
                                            String dat = ByteUtils.bytesArrayToUtf8(dataTemp);
                                            TagMomeory tagMomeory = new TagMomeory(SelectDataView.observableList.get(j).getIsStatic(),SelectDataView.observableList.get(j).getHexData().toUpperCase(),""+dat,SelectDataView.observableList.get(j).getBaseAddress(),SelectDataView.observableList.get(j).getTypeData(),""+dataTemp.length);
                                            observableList.add(tagMomeory);
                                        }
                                    }
                                }
                            }
                        }

                        SelectDataView.observableList = observableList;
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                SelectDataView.tableView.setItems(SelectDataView.observableList);
                            }
                        });
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };
        thread1 = new Thread(runnable1);
        thread1.start();
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(SelectDataView.observableList.size()>0 && OpenProcessListView.processHandler>0){
                    if(!flag){
                        flag=true;
//                        SelectDataView.tableView.setMouseTransparent(true);
                        button.setText("停止列表刷新");
                    }else{
                        flag=false;
//                        SelectDataView.tableView.setMouseTransparent(false);
                        button.setText("开启列表刷新");

                    }
                }else {
                    SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"刷新失败,请先确认是否首次搜索","温馨提示",0);
                }


            }
        });
    }
}
